define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'food/ingredient/index' + location.search,
                    add_url: 'food/ingredient/add',
                    edit_url: 'food/ingredient/edit',
                    del_url: 'food/ingredient/del',
                    multi_url: 'food/ingredient/multi',
                    import_url: 'food/ingredient/import',
                    table: 'food_ingredient',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'asc',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'categories.name', title: __('Categories.name'), operate: 'LIKE'},
                        {field: 'smallimage', title: __('Smallimage'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'weight', title: __('Weight'), operate:'BETWEEN'},
                        {field: 'edible_portion', title: __('Edible_portion'), operate:'BETWEEN'},
                        {field: 'water_content', title: __('Water_content'), operate:'BETWEEN'},
                        {field: 'energy_kilocalorie', title: __('Energy_kilocalorie'), operate:'BETWEEN'},
                        {field: 'energy_kj', title: __('Energy_kj'), operate:'BETWEEN'},
                        {field: 'protein', title: __('Protein'), operate:'BETWEEN'},
                        {field: 'fat', title: __('Fat'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"30":__('Status 30')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            // 绑定自动分类按钮事件
            $(document).on('click', '#btn-auto-classify', function () {
                Fast.api.ajax({
                    url: 'food/ingredient/autoClassify',
                    type: 'POST'
                }, function(data, ret){
                    // 成功回调
                    Layer.alert(ret.msg, {title: '处理结果'});
                    // 刷新表格
                    table.bootstrapTable('refresh');
                }, function(data, ret){
                    // 失败回调
                    Layer.alert(ret.msg, {title: '处理失败'});
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});